﻿using Blm.VisionCloud.FrameWork.Cached;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.Data;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Filters;
using VisionSurface.Service.Ms;

namespace VisionSurface.Web.Controllers.Ms
{
    /// <summary>
    /// 项目管理
    /// </summary>
    [ApiVersion("1.0")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiExplorerSettings(GroupName = "项目管理")]
    [Route("api/miniapp/ms/[controller]/[Action]")]
    public class ProjectController : ApiBaseController
    {
        public ICached Cached { get; set; }

        public ProjectService projectService { get; set; }

        public AlgoTemplateService goTemplateService { get; set; }

        /// <summary>
        /// 获取项目的楼栋楼层
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(ProjectBuildingFloorDto))]
        public async ValueTask<ApiResult> GetFloors([FromBody] ProjectIDRequiredDto dto)
        {
            var list = await projectService.GetProjectBuildingFloorsAsync(dto.ProjectId);
            return new ApiResult { Code = AjaxCode.Success, Result = list };
        }

        /// <summary>
        /// 获取项目的户型图列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(HousemapBuildingFloorResponseDto))]
        public async ValueTask<ApiResult> GetHouseMaps([FromBody] ProjectIDRequiredDto dto)
        {
            return await projectService.GetHouseMapAndFloorsAsync(dto.ProjectId);
        }

        /// <summary>
        /// 获取项目的算法模板
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(MsAlgoTempDto))]
        public async ValueTask<ApiResult> GetAlgorithmTemplates([FromBody] ProjectIDRequiredDto dto)
        {
            var list = await goTemplateService.GetAlgorithmParamTemplateListAsync(dto.ProjectId);
            return new ApiResult { Code = AjaxCode.Success, Result = list?.Select(p => new { p.AlgorithmParamTemplateGuid, p.TemplateName, p.TemplateType }) };
        }

        /// <summary>
        /// 获取测量项的设计模板
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(MsMeasureTemplateDto))]
        public async ValueTask<ApiResult> GetMeasureTemplates([FromBody] HouseMapIdDto dto)
        {
            var list = await projectService.GetMsMeasureItemTemplatesAsync(dto.HouseMapId);
            return new ApiResult { Code = AjaxCode.Success, Result = list };
        }

        /// <summary>
        /// 获取对应户型图和工程阶段的测量项
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(MsPointTypeDto))]
        public async ValueTask<ApiResult> GetMeasureItems([FromBody] PointTypeDetailRequestDto dto)
        {
            var list = await projectService.GetPointTypeDetailsAsync(dto);
            return new ApiResult { Code = AjaxCode.Success, Result = list };
        }

        /// <summary>
        /// 获取工程阶段
        /// </summary>
        /// <param name="ProjectId">项目id</param>
        /// <param name="addDefault"></param>
        /// <returns></returns>
        [HttpGet, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(SelectItem))]
        public ApiResult GetProjectStages(Guid ProjectId, bool addDefault = false)
        {
            var list = Cached.GetOrSet(GetOpenApiName(), () =>
            {
                var list = DataStore.GetStandards(ProjectId);
                var items = list.Select(w => new SelectItem(w.Stage, w.ProjectStandardId.ToString())).ToArray();
                return SelectItem.ToItems(items, addDefault);
            }, DataStore.GetCommonTimeSpan());

            return new ApiResult { Code = AjaxCode.Success, Result = list };
        }

        /// <summary>
        /// 获取机器测量项和人工测量项
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>
        /// 返回内容说明
        ///  ArtificialStandard：人工测量项
        ///  RobotStandard：机器测量项
        /// </remarks>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(StandardListResponseDto))]
        public async ValueTask<ApiResult> GetStageStandards(StageStandardQueryDto dto)
        {
            return await projectService.GetStageStandardsAsync(dto);
        }

        /// <summary>
        /// 获取建筑工艺
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(SelectItem))]
        public ApiResult GetBuildingCraft(BuildingCraftRequestDto dto)
        {
            var cache = Cached.GetOrSet(GetOpenApiName(), () =>
            {
                var list = DataStore.GetStandards(dto.ProjectId);
                return list.Select(w => new SelectItem(w.Stage, w.ProjectStandardId.ToString())).ToList();
            }, DataStore.GetCommonTimeSpan());

            var standardEntity = cache.Single(w => w.Value == dto.StandardId.ToString());
            var items = Dict.BuildingCraft.Where(w => w.FirstLv.Equals(standardEntity.Text)).Select(w => new SelectItem(w.SecondLv, w.SecondLv)).ToArray();
            var result = SelectItem.ToItems(items, dto.AddDefault);

            return new ApiResult { Code = AjaxCode.Success, Result = result.Where(p => !string.IsNullOrWhiteSpace(p.Value)) };
        }

        /// <summary>
        /// 获取户型图Url
        /// </summary>
        [HttpPost, SwaggerOperation(Tags = new[] { "项目管理" })]
        [SwaggerResponse(0, "Result", typeof(string))]
        public async Task<ApiResult> GetHouseMapUrlById(HouseMapIdDto dto)
        {
            var res = await projectService.GetHouseMapUrlById(dto.HouseMapId);
            return new ApiResult { Code = AjaxCode.Success, Result = res };
        }
    }
}